package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VoiceApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Voice;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.VoiceVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
public class VoiceService {

    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;

    @DubboReference
    private VoiceApi voiceApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //发送语音文件
    public void sendVoice(MultipartFile soundFile) throws IOException {
        //获取redis中音频的次数
        String key = Constants.SEND_VOICE_KEY + UserHolder.getUserId();
        if(!redisTemplate.hasKey(key)){
            redisTemplate.opsForValue().set(key,"10",1, TimeUnit.DAYS);
        }
        String number = redisTemplate.opsForValue().get(key);
        Integer count = Integer.valueOf(number);
        if(count < 1){
            throw new BusinessException(ErrorResult.builder().errMessage("今日发送音频已满,明日再来!").build());
        }
        //1、将语音文件上传到FastDFS,获取访问URL
        String filename = soundFile.getOriginalFilename();  // abc.mp3
        filename = filename.substring(filename.lastIndexOf(".")+1);
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), filename, null);
        String voiceUrl = webServer.getWebServerUrl() + storePath.getFullPath();
        //2、构建Voice对象
        Voice voice = new Voice();
        voice.setUserId(UserHolder.getUserId());
        voice.setVoiceUrl(voiceUrl);
        //调用api保存数据
        String voiceId = voiceApi.save(voice);
        if(StringUtils.isEmpty(voiceId)){
            throw new BusinessException(ErrorResult.error());
        }
        //结束时修改次数,存入redis中
        count = count - 1;
        redisTemplate.opsForValue().set(key,String.valueOf(count));
    }

    //接收语音
    public VoiceVo getVoice() {
        //获取redis中音频的次数
        String key = Constants.GET_VOICE_KEY + UserHolder.getUserId();
        if(!redisTemplate.hasKey(key)){
            redisTemplate.opsForValue().set(key,"10",1, TimeUnit.DAYS);
        }
        String number = redisTemplate.opsForValue().get(key);
        Integer count = Integer.valueOf(number);
        if(count < 1){
            throw new BusinessException(ErrorResult.builder().errMessage("今日接收音频已满,明日再来!").build());
        }
        //获取音频
        Voice voice = voiceApi.getVideo(UserHolder.getUserId());
        //获取用户的信息
        UserInfo userInfo = userInfoApi.findById(voice.getUserId());
        //封装对象返回
        if(userInfo == null){
            return new VoiceVo();
        }
        VoiceVo voiceVo = VoiceVo.init(userInfo, voice);
        voiceVo.setRemainingTimes(count);
        //结束时修改次数,存入redis中
        count = count - 1;
        redisTemplate.opsForValue().set(key,String.valueOf(count));
        return voiceVo;
    }
}
